<?php
/**
 * V5
 *
 */
class data_follow_group extends data {
	public function __construct() {
		parent::__construct();
		
	}
	/**
	 * 添加一个分组
	 *
	 * @param int $uid
	 * @param string $title					// 没有转义
	 * @param string $privacy
	 * @return int
	 */
	public function add ($uid, $title, $privacy) {
		$uid		= intval($uid);
		$title		= addslashes(strip_tags(trim($title)));
		$privacy	= ($privacy==='private') ? $privacy : 'public';
		
		if ($uid and $title) {
			$insert_id = $this->db->execute('
				insert into
					follow_group
				set
					user_id			= "'.$uid.'",
					title			= "'.$title.'",
					privacy			= "'.$privacy.'",
					total_source	= "0"
			');
			return $insert_id;
		} else return false;
	}
	/**
	 * 修改分组的标题和隐私设置
	 *
	 * @param int $uid
	 * @param int $id
	 * @param string $title				// 没有转义
	 * @param string $privacy
	 * @return int
	 */
	public function edit ($uid, $id, $title, $privacy) {
		$uid		= intval($uid);
		$id			= intval($id);
		$title		= addslashes(strip_tags(trim($title)));
		
		if ($uid and $id and $title and ($privacy==='public' or $privacy==='private')) {
			$update = $this->db->execute('
				update
					follow_group
				set
					title 		= "'.$title.'",
					privacy 	= "'.$privacy.'"
				where
					id		= "'.$id.'"		and
					user_id	= "'.$uid.'"
			');
			return $update;
		} else return false;
	}
	/**
	 * 删除一个分组
	 *
	 * @param int $uid
	 * @param int $id
	 * @return int
	 */
	public function del ($uid, $id) {
		$uid 	= intval($uid);
		$id		= intval($id);
		
		if ($uid and $id) {
			$group = $this->db->execute('
				select
					id
				from
					follow_group
				where
					id		= "'.$id.'"		and
					user_id	= "'.$uid.'"
			');
			if ($group) {
				// 记录订阅源，发送给event以删除F中的内容
				$source = $this->db->execute('
					select
						source_type, source_id
					from
						follow_source
					where
						group_id	= "'.$id.'"		and
						user_id		= "'.$uid.'"
				');
				$data_event = new data_event();
				$data_event->add('del_follow_group', $uid, $source);		// 删除tee中的记录
				// 删除订阅文章
				$this->db->execute('
					delete from 
						follow_article
					where
						group_id	= "'.$id.'"		and
						user_id		= "'.$uid.'"
				');
				
				// 删除订阅源
				$this->db->execute('
					delete from 
						follow_source
					where
						group_id	= "'.$id.'"		and
						user_id		= "'.$uid.'"
				');
				// 删除组
				$del = $this->db->execute('
					delete from 
						follow_group
					where
						id		= "'.$id.'"		and
						user_id	= "'.$uid.'"
				');
				return $del;
			} else return false;
		} else return false;
	}
	/**
	 * 获得一条记录
	 *
	 * @param int $uid
	 * @param int $id
	 * @return array
	 */
	public function one ($uid, $id) {
		$uid	= intval($uid);
		$id		= intval($id);
		
		if ($uid and $id) {
			$execute = $this->db->execute('
				select
					*
				from
					follow_group
				where
					id	= "'.$id.'"	and
					(privacy = "public" or user_id = "'.$uid.'")
			');
			return $execute[0];
		} else return false;
	}
	/**
	 * 列出订阅分组
	 *
	 * @param int $uid
	 * @param String $relation		// public / private
	 * @return Array
	 */
	public function ls ($uid, $relation) {
		$uid		= intval($uid);
		$relation	= ($relation === 'private') ? $relation : 'public';
		
		if ($uid) {
			$execute = $this->db->execute('
				select
					*
				from
					follow_group
				where
					follow_group.user_id		= "'.$uid.'"
					'.(($relation==='private') ? '' : ' and privacy="public" ').'
				order by
					follow_group.id ASC
			');
			return $execute;
		} else return false;
	}
	/**
	 * 统计组内的订阅源的总数
	 *
	 * @param int $group_id
	 * @param int $number			// 非0时直接加减该数值
	 * @return int
	 */
	public function count_total ($group_id, $number=0) {
		$group_id 	= intval($group_id);
		$number		= intval($number);
		if ($group_id) {
			if ($number > 0) {
				$update = $this->db->execute('
					update
						follow_group
					set
						total_source = total_source + '.$number.'
					where
						id = "'.$group_id.'"
				');
			} elseif ($number < 0) {
				$update = $this->db->execute('
					update
						follow_group
					set
						total_source = total_source - '.abs($number).'
					where
						id = "'.$group_id.'"
				');
			} else {
				$update = $this->db->execute('
					update
						follow_group
					set
						total_source = (select COUNT(*) from follow_source where group_id="'.$group_id.'")
					where
						id = "'.$group_id.'"
				');
			}
			return $update;
		} else return false;
	}
}
?>